﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Logistics.DataManage.DAL;
using System.Data;
using Logistics.DataManage.Local;
using MapgisEbp.QuickDataBase;
using Logistics.Utils;

namespace Logistics.DataManage
{
    class UserDAO
    {
        public static IDictionary<String, String> GetUserBySiteId(String SiteId)
        {
            //String sql = "select Users.UserId,UserName from Users,usersites where NetSiteId=? and Users.UserId=usersites.UserId";
            String sql = "select Users.UserId,UserName from Users,usersites where Users.UserId=usersites.UserId";
            sql += " and usersites.NetSiteId in (select NetWorkId from NetWorkInfo where SiteId=?)";

            ParameterizedSQL PSQL = LocalDb.Handler.DbAdapter.AdaptSQLAnonymousParams(sql, SiteId);
            DataSet ds = LocalDb.Handler.ExecuteQuery(PSQL.SQL, PSQL.Parameters);
            return CollectionUtils.FillDictionary(ds.Tables[0], "UserID", "UserName", false);
        }
        public static OUser IsUserExitInSite(string userId, string siteId)
        {
            String sql = "select * from Users u left join USERSITES s on u.UserId=s.UserId where u.UserId=? and s.netsiteid=? and u.isused='1' COLLATE NOCASE";
            ParameterizedSQL PSQL = LocalDb.Handler.DbAdapter.AdaptSQLAnonymousParams(sql, userId,siteId);
            DataSet ds = LocalDb.Handler.ExecuteQuery(PSQL.SQL, PSQL.Parameters);
            IList<OUser> list = ConvertTo(ds);
            return list.Count == 0 ? null : list[0];
        }
        public static OUser GetUserByUserName(String userId)
        {
            String sql = "select * from Users where UserId=? COLLATE NOCASE";
            ParameterizedSQL PSQL = LocalDb.Handler.DbAdapter.AdaptSQLAnonymousParams(sql, userId);
            DataSet ds = LocalDb.Handler.ExecuteQuery(PSQL.SQL, PSQL.Parameters);
            IList<OUser> list = ConvertTo(ds);
            return list.Count == 0 ? null : list[0];
        }
        /// <summary>
        /// 获取用户name
        /// </summary>
        /// <returns></returns>
        public static string GetUserRealName(string UserId)
        {
            OUser user = GetUserByUserName(UserId);
            return user == null ? "" : user.UserName;
        }

        public static String GetSiteIdbyUserId(String userId)
        {
            String sql = "select NetSiteId from usersites where UserId = ? COLLATE NOCASE";
            ParameterizedSQL PSQL = LocalDb.Handler.DbAdapter.AdaptSQLAnonymousParams(sql, userId);
            Object obj = LocalDb.Handler.ExecuteScalar(PSQL.SQL, PSQL.Parameters);
            return obj == null ? "" : obj.ToString();
        }

        private static IList<OUser> ConvertTo(DataSet ds)
        {
            IList<OUser> list = new List<OUser>();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    OUser user = new OUser();
                    user.UserName = Convert.ToString(row["UserName"]);
                    user.UserID = Convert.ToString(row["UserID"]);
                    user.UserPwd = Convert.ToString(row["UserPwd"]);
                    String isUserd = Convert.ToString(row["IsUsed"]);
                    user.IsUsed = !(isUserd == "0");// Convert.ToBoolean(String.IsNullOrEmpty(isUserd) ? "true" : isUserd);

                    list.Add(user);
                }
            }
            return list;
        }

    }
}
